package model;
import tools.*;
import java.io.*;
//我们用“航班编号.txt”的文件名创建了储存航班信息的txt文件，例如“BA0570.txt”。
//eachFlight类 使我们能够查看航班文件中的信息，还能更改航班座位的剩余量
public class eachFlight {
    public static String boardingTime(String content){//括号中添加所要查询的“航班编号”，该函数返回该航班的登机时间
        String boarding_time=null;
        lineReader lr = new lineReader();
        File file = new File("resource/"+content+".txt");
        if(file.exists()) {
          String boardingTime_line = lr.readLine("resource/" + content + ".txt", "BOARDINGTIME");
           String[] sp = boardingTime_line.split("\\s+");
           boarding_time=sp[1];
        }else{
            System.out.println("There is no flight called "+content);
        }
        return boarding_time;
    }

    public static String COMPANY(String content){//括号中添加所要查询的“航班编号”，该函数返回该航班隶属的航空公司
        String COMPANY="";
        lineReader lr = new lineReader();
        File file = new File("resource/"+content+".txt");
        if(file.exists()) {
            String COMPANY_line = lr.readLine("resource/" + content + ".txt", "COMPANY");
            String[] sp = COMPANY_line.split("\\s+");
            COMPANY=sp[1];
            int i = 2;
            while(i<sp.length) {
                COMPANY = COMPANY + " "+sp[i];
                i = i + 1;
            }
        }else{
            System.out.println("There is no flight called "+content);
        }
        return COMPANY;
    }

    public static String GATE(String content){//括号中添加所要查询的“航班编号”，该函数返回该航班的登机口号
        String GATE_number=null;
        lineReader lr = new lineReader();
        File file = new File("resource/"+content+".txt");
        if(file.exists()) {
            String GATE_line = lr.readLine("resource/" + content + ".txt", "GATE");
            String[] sp = GATE_line.split("\\s+");
            GATE_number=sp[1];
        }else{
            System.out.println("There is no flight called "+content);
        }
        return GATE_number;
    }

    public static String IMG(String content){//括号中添加所要查询的“航班编号”，该函数返回该航班抵达目的地的“图片名”
        String img="";
        lineReader lr = new lineReader();
        File file = new File("resource/"+content+".txt");
        if(file.exists()) {
            String img_line = lr.readLine("resource/" + content + ".txt", "IMG");
            String[] sp = img_line.split("\\s+");
            img=sp[1];
            int i = 2;
            while(i<sp.length) {
                img = img + " "+sp[i];
                i = i + 1;
            }
        }else{
            System.out.println("There is no flight called "+content);
        }
        return img;
    }

    public static String DEPARTURE(String content){//括号中添加所要查询的“航班编号”，该函数返回该航班的始发地
        String DEPARTURE=null;
        lineReader lr = new lineReader();
        File file = new File("resource/"+content+".txt");
        if(file.exists()) {
            String DEPARTURE_line = lr.readLine("resource/" + content + ".txt", "DEPARTURE");
            String[] sp = DEPARTURE_line.split("\\s+");
            DEPARTURE=sp[1];
        }else{
            System.out.println("There is no flight called "+content);
        }
        return DEPARTURE;
    }
    public static String DESTINATION(String content){//括号中添加所要查询的“航班编号”，该函数返回该航班的目的地
        String DESTINATION=null;
        lineReader lr = new lineReader();
        File file = new File("resource/"+content+".txt");
        if(file.exists()) {
            String DESTINATION_line = lr.readLine("resource/" + content + ".txt", "DESTINATION");
            String[] sp = DESTINATION_line.split("\\s+");
            DESTINATION=sp[1];
        }else{
            System.out.println("There is no flight called "+content);
        }
        return DESTINATION;
    }

    public static String DEPARTS(String content){//括号中添加所要查询的“航班编号”，该函数返回该航班的始发时间
        String DEPARTS=null;
        lineReader lr = new lineReader();
        File file = new File("resource/"+content+".txt");
        if(file.exists()) {
            String DEPARTS_line = lr.readLine("resource/" + content + ".txt", "DEPARTS");
            String[] sp = DEPARTS_line.split("\\s+");
            DEPARTS=sp[1];
        }else{
            System.out.println("There is no flight called "+content);
        }
        return DEPARTS;
    }

    public static String ARRIVES(String content){//括号中添加所要查询的“航班编号”，该函数返回该航班的抵达时间
        String ARRIVES=null;
        lineReader lr = new lineReader();
        File file = new File("resource/"+content+".txt");
        if(file.exists()) {
            String ARRIVES_line = lr.readLine("resource/" + content + ".txt", "ARRIVES");
            String[] sp = ARRIVES_line.split("\\s+");
            ARRIVES=sp[1];
        }else{
            System.out.println("There is no flight called "+content);
        }
        return ARRIVES;
    }

    public static String DELAY(String content){//括号中添加所要查询的“航班编号”，该函数返回一个数值，若为0表示该航班延迟起飞，若为1表示该航班正常起飞
        String DELAY=null;
        lineReader lr = new lineReader();
        File file = new File("resource/"+content+".txt");
        if(file.exists()) {
            String DELAY_line = lr.readLine("resource/" + content + ".txt", "DELAY");
            String[] sp = DELAY_line.split("\\s+");
            DELAY=sp[1];
        }else{
            System.out.println("There is no flight called "+content);
        }
        return DELAY;
    }
    //该函数可以查看被选中座位是否已被选择，括号中添加所要查询的“航班编号”“座位排数（字母seatA-seatF）”座位列数（数字01-20）“
    //若返回数值为1，代表该座位为空座，还能被选择。若返回数值为0，代表该作为已被选用，不能再被选择。
    public static int seat(String content,String letter,String number){
        int judgement = 0;
        lineReader lr = new lineReader();
        File file = new File("resource/"+content+".txt");
        if(file.exists()) {
            String seat_line = lr.readLine("resource/" + content + ".txt", letter);
            String[] sp = seat_line.split("\\s+");
            int i=0;
            while(i<sp.length){
                if(sp[i].equals(number)){
                    judgement = 1;
                    break;
                }
                i=i+1;
            }
        }else{
            System.out.println("There is no flight called "+content);
        }
        return judgement;
    }
//该函数可用于座位的选择，括号中变量添加所选择的”航班编号““座位排数（字母seatA-seatF）”座位列数（数字01-20）“
//座位被选择后，文本中的数据”数字01-20“会被替换成”chosen“
    public static void seat_choose(String content,String letter,String number){
        lineReader lr = new lineReader();
        lineWriter lw = new lineWriter();
        int lineNum=lr.findLine("resource/"+content+".txt",letter);
        String reLine = lw.lineRewrite("resource/"+content+".txt",lineNum,number,"chosen");
        lw.writeLine("resource/"+content+".txt",lineNum,reLine);
    }
}
